<?php
require_once('include/inc_database.php');
require_once('include/inc_useraccount.php');
require_once('include/inc_recommendation.php');
require_once('include/inc_reccomment.php');

// Open DB connection.
$dbobj = new DatabaseObject();
$dbobj->OpenConnection();

$username = $_GET['user'];

$user_ar = $dbobj->GetResultsAsNestedArray("SELECT * FROM mr_useraccount WHERE username = " . ValToSQL_str($username) . ";", 1);
$user_r = $user_ar[0];

$auth = new UserAccount();


$tab = $_GET['tab'];
if (!$tab) {
	if ($username === $auth->AuthenticatedUser())
		$tab = "in";
	else
		$tab = "out";
}

$PAGE_LENGTH = 10;
$page = $_GET['page'];
if (!$page)
	$page = 1;
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><?=SiteConst('site_name')?> - <?=$username?></title>

<link rel="shortcut icon" href="images/favicon.gif" type="image/x-icon">
<meta name="viewport" content="width=700"/>

<?if ($username === $auth->AuthenticatedUser()) {?>
	<link rel="alternate" type="application/rss+xml" title="My subscription feed" href="rec_feed.php?user_in=<?=$username?>">
	<link rel="alternate" type="application/rss+xml" title="My comment feed" href="comment_feed.php?user_sub=<?=$username?>">
	
<?}?>

<link rel="alternate" type="application/rss+xml" title="<?=SiteConst('site_name')?> - <?=$username?>" href="rec_feed.php?user=<?=$username?>">


<script type="text/javascript">
	//document.write("Hello World!");
	function maketoday() {
		document.mainform.updateDate.value = document.mainform.tempDate.value;
		//document.mainform.updateDate.color = "#000088";
	}
	function makeNow(destination_field) {
		//var text_now = "<?=date('Y-m-d H:i:s', time())?>";
		var text_now = "<?=date('Y-m-d H:i', time())?>";
		destination_field.value = text_now;
	}

	function ConfirmSubscribe() {
		var temp;
		temp = confirm("Subscribe: recommendations from <?=$username?> will be included in your own subscription feed.");
		return temp;
	}

</script>

<style type="text/css">

	@import url('rec.css?<?=$CSS_VERSION?>');

	/* Tab style */
	table.tabs {
		margin: 0px;
		padding: 0px;
		font-family: sans-serif;
		border-spacing: 0px;
	}

	table.tabs td {
		color: #000000;
		border: solid 0px #000000;
		padding: 4px 10px 5px;
		white-space: nowrap;
	}

	table.tabs td:hover {
		border: dashed 1px #fff;
		border-bottom-width: 0px;
		padding: 3px 9px 5px;
	}

	table.tabs td.selected_tab {
		font-weight: bold;
		font-style: italic;
		color: #555555;

		background-color: #fff;
		border: solid 1px #ccc;
		/*border-right-color: #aaa;*/
		border-bottom-width: 0px;
		padding: 3px 9px 5px;
	}

	div.tab_backdrop {
		margin:5px 0 5px -10px;
		background-color:#ddd;
		/*background-image: url('images/tab_backdrop.png');
		background-repeat: repeat-x; 
		background-position:0 100%;*/
		padding:10px 15px 0;
		
	}

	/* Comment */
	div.cmt_class { margin: 15px 0; }

	div.cmt_class .cmt_info { color: #999; }


	/* No recs */
	div.no_recs {
		margin: 40px 40px;
		text-align: center;
		font-family: serif;
		font-style: italic;
		font-size: 200%;
		color: #aaa;
	}

	/* Form */
	tr.rec_form_row > td {
		padding-bottom: 8px;
	}

	tr.rec_form_row .info_only {
		color: #888;
		font-style: italic;
	}

</style>
</head>

<body>

<div style="float:right;">
	<?=UserAccount::AccountNavHtml()?>
</div>

<h1 style="margin-bottom:0px;">
	<a href="./"><img src="images/logo02b.jpg" align="middle" border="0"/></a>
	<?=SiteConst('site_name')?>
</h1>

	
<table style="width:100%;">
<tr>
<td style="padding-right:10px;">

	<div style="margin-top:-15px;">
		<?if ($user_r['userIconPath'] != "") {?>
			<div style="float:right;">
				<img src="image.php?usericon=<?=$user_r['userIconPath']?>&amp;size=64" style="margin:0 20px;"/>
			</div>
		<?}?>
		<h1 style="margin:0px; text-align:right;"><?=$username?></h1>
	</div>
	<div style="" class="tab_backdrop">
	<table class="tabs">
	<tr>
		<?
		if ($tab == "out")
			echo "<td class='selected_tab'><img src='images/rec_out02.png' align='top'>&nbsp;Recommendations</td>";
		else
			echo "<td><img src='images/rec_out02.png' align='top'>&nbsp;<a href='user.php?user=" . $username . "&amp;tab=out'>Recommendations</a></td>";
		if ($tab == "in")
			echo "<td class='selected_tab'><img src='images/rec_in02.png' align='top'>&nbsp;Subscription&nbsp;feed</td>";
		else
			echo "<td><img src='images/rec_in02.png' align='top'>&nbsp;<a href='user.php?user=" . $username . "&amp;tab=in'>Subscription feed</a></td>";
		if ($tab == "comments")
			echo "<td class='selected_tab'>Comments</td>";
		else
			echo "<td><a href='user.php?user=" . $username . "&amp;tab=comments'>Comments</a></td>";
		?>
	</tr>
	</table>
	</div>

	<?if ($tab == "out") {?>
		<div class="inset_box" style="margin:20px 0 35px; font-style:italic;">
			<?if ($username === $auth->AuthenticatedUser()) {?>
				<b>Outgoing recommendations:</b> Recommendations you made.
			<?}
			else {
				$temp = $dbobj->GetFirstCell("SELECT COUNT(*) FROM mr_subscription WHERE username = " . ValToSQL_str($auth->AuthenticatedUser()) . " AND subscribedToUsername = " . ValToSQL_str($username) . "");
				if ($temp > 0)
					$is_subscribed = true;
				else
					$is_subscribed = false;
				?>
				<b>Outgoing recommendations:</b> All recommendations made by <?=$username?>.
				<div class="inset_box">
					<!--<h6 style="margin-bottom:5px;">Get notified of new recommendations:</h6>-->
					
					<div style="margin:5px 0px 5px;">
						<div style="margin-top:5px;">
							<?if ($is_subscribed) {?>
								<img src="images/success_check01.png" align="top"/>  You are subscribed to <?=$username?>'s recommendations.
							<?}
							else {?>
								&bull; <b><a onclick="return ConfirmSubscribe();" href="subscription_add.php?subscribeTo=<?=$username?>">Subscribe</a></b> to these outgoing recommendations<!-- (by <?=$username?>)-->.
							<?}?>
						</div>
						<!--<div style="margin-top:5px;">
							&bull; Or, subscribe directly to <a href="rec_feed.php?user=<?=$username?>">this recommendation feed</a> in a feed reader.
						</div>-->
					</div>
				</div>
			<?}?>
		</div>

		<?
		$rec_ar = Recommendation::GetRecArray("user_out:" . $username, $PAGE_LENGTH, $page-1, true, $row_count);
		?>
		
		<?$i = 0;
		foreach ($rec_ar as $rec_r) {
			$i++;?>
			<div class="rec_class" id="rec<?=$rec_r['recID']?>">
				<div style="font-weight:bold;"><?=htmlentities($rec_r['recText'])?></div>
				<div><?=Recommendation::DescriptionHtml($rec_r['description'])?></div>
				<div class="rec_info">
					<span class="rec_heading"><?=date("Y-m-d", strtotime($rec_r["addedDateTime"]))?></span>
					&nbsp;<a href="rec.php?recID=<?=$rec_r['recID']?>"><?=$rec_r['comment_count']?> comments</a>
				</div>
			</div>
		<?}
		if ($i == 0) {
			echo "<div class='no_recs'>No outgoing recommendations yet.</div>";
		}

		// Page links
		$page_count = ceil($row_count/$PAGE_LENGTH);
		if ($page_count > 0)
			echo "Page: ";
		for ($i=0; $i<$page_count; $i++) {
			$temp_page_number = $i+1;
			if ($temp_page_number == $page)
				echo "<b>" . $temp_page_number . "</b> &nbsp;";
			else
				echo "<a href='user.php?user=" . $username . "&amp;tab=" . $tab . "&amp;page=" . $temp_page_number . "'>" . $temp_page_number . "</a> &nbsp;";
		}

	}

	// Incoming
	elseif ($tab == "in") {?>

		<!--<h6 style="margin:15px 0;">Incoming Recommendations:</h6>-->
		<div class="inset_box" style="margin:20px 0 35px; font-style:italic;">
			<?if ($username === $auth->AuthenticatedUser()) {?>
				<b>Subscription feed:</b> Recommendations from all users to whom you're subscribed.&nbsp; Be notified when your subscription feed is updated by email (in <a href="user_settings.php">account settings</a>), or by rss (<a href="rec_feed.php?user_in=<?=$username?>">feed</a> <a href="rec_feed.php?user_in=<?=$username?>"><img src="images/feed_icon14.png" align="top" border="0" alt="feed"/></a>).
			<?}
			else {?>
				<b>Subscription feed:</b> Recommendations from all users to whom <?=$username?> is subscribed.
			<?}?>
		</div>

		<?
		$rec_ar = Recommendation::GetRecArray("user_in:" . $username, $PAGE_LENGTH, $page-1, true, $row_count);
		
		$i = 0;
		foreach ($rec_ar as $rec_r) {
			$i++;?>
			<div class="rec_class" id="rec<?=$rec_r['recID']?>">
				<div style="font-weight:bold;"><?=htmlentities($rec_r['recText'])?></div>
				<div><?=Recommendation::DescriptionHtml($rec_r['description'])?></div>
				<div class="rec_info">
					<span class="rec_heading"><?=date("Y-m-d", strtotime($rec_r["addedDateTime"]))?></span>
					by
					<?if ($rec_r['userIconPath'] != "") {
						echo "<a href='user.php?user=" . $rec_r['username'] . "'><img src='image.php?usericon=" . $rec_r['userIconPath'] . "&amp;size=16' align='ABSMIDDLE' style='border:solid 1px #aaa; padding:1px;'/></a>";
					}?>
					<a href="user.php?user=<?=$rec_r['username']?>"><?=htmlentities($rec_r['username'])?></a>
					&nbsp; &nbsp;<a href="rec.php?recID=<?=$rec_r['recID']?>"><?=$rec_r['comment_count']?> comments</a>
				</div>
			</div>
		<?}
		if ($i == 0) {
			echo "<div class='no_recs'>No incoming recommendations yet.</div>";
			if ($username === $auth->AuthenticatedUser() && $dbobj->GetFirstCell("SELECT COUNT(*) FROM mr_subscription sub WHERE username = " . ValToSQL_str($username) . ";") == 0) {
				echo "<div style='margin:-30px auto 40px; text-align:center; font-style:italic;'>(You are not subscribed to any users.)</div>";
			}
		}
		
		// Page links
		$page_count = ceil($row_count/$PAGE_LENGTH);
		if ($page_count > 0)
			echo "Page: ";
		for ($i=0; $i<$page_count; $i++) {
			$temp_page_number = $i+1;
			if ($temp_page_number == $page)
				echo "<b>" . $temp_page_number . "</b> &nbsp;";
			else
				echo "<a href='user.php?user=" . $username . "&amp;tab=" . $tab . "&amp;page=" . $temp_page_number . "'>" . $temp_page_number . "</a> &nbsp;";
		}
	
	}

	// Comments
	elseif ($tab == "comments") {?>

		<div class="inset_box" style="margin:20px 0 35px; font-style:italic;">
			<b>Comment feed:</b> all comments from <?=$username?>'s recommendation and subscription feeds.
			<?if ($username === $auth->AuthenticatedUser()) {?>
				<b><a href="comment_feed.php?user_sub=<?=$username?>">Subscribe to this feed</a></b> in a feed reader to get notified when new comments appear here.
			<?}
			else {?>
			<?}?>
		</div>


		<?
		$cmt_ar = $dbobj->GetResultsAsNestedArray("SELECT cmt.*, " .
			"rec.recID, rec.username AS rec_username, rec.artist, usr.userIconPath " .
			"FROM mr_recComment cmt " .
			"INNER JOIN mr_useraccount usr ON cmt.username = usr.username " .
			"INNER JOIN mr_recommendation rec ON cmt.recID = rec.recID " .
			"WHERE (rec.username = " . ValToSQL_str($username) . " " .
				"OR rec.username IN (SELECT sub.subscribedToUsername FROM mr_subscription sub " .
					"WHERE sub.username = " . ValToSQL_str($username) . ")) " .
			"AND rec.disabled = 0 AND cmt.disabled = 0 " .
			"ORDER BY cmt.commentDate DESC", 100);
		?>
		
		<?$i = 0;
		foreach ($cmt_ar as $cmt_r) {
			$i++;?>
			<div class="cmt_class" id="comment<?=$cmt_r['commentNumber']?>">
				<div class="cmt_info">
					<?if ($cmt_r['userIconPath'] != "") {
						echo "<a href='user.php?user=" . $cmt_r['username'] . "'><img src='image.php?usericon=" . $cmt_r['userIconPath'] . "&amp;size=16' align='ABSMIDDLE' style='border:solid 1px #aaa; padding:1px;'/></a>";
					}?>
					<b><i><a href="user.php?user=<?=$cmt_r['username']?>"><?=$cmt_r['username']?></a> said,</i></b> of <a href="rec.php?recID=<?=$cmt_r['recID']?>">rec. by <?=$cmt_r['rec_username']?></a> (<?=$cmt_r['artist']?>)
					&nbsp;<?=date("Y-m-d H:i:s", strtotime($cmt_r["commentDate"]))?>
				</div>
				<div><?=RecComment::CommentFormat($cmt_r['content'])?></div>
			</div>
		<?}
		if ($i == 0) {
			echo "<div class='no_recs'>No comments yet.</div>";
		}
	
	}?>
	

	<hr/>


	<?
	// Add recommendation form.
	if ($username === $auth->AuthenticatedUser()) {?>
		<div id="add_rec" style="margin:30px 15px 20px 5px; border:solid 1px #aaa; padding:6px;">
		<form name="add_form" method="post" action="rec_add.php" onsubmit="document.add_form.robot_detector.value = 'abc' + 'fed';">
			<input type="hidden" name="add_rec" VALUE="on">
			<input type="hidden" name="username" VALUE="<?=$username?>">
			<input type="hidden" name="robot_detector" VALUE="robot">

			<h5><img src="images/rec_add02b.png" align="top"/> Add recommendation:</h5>
			<div>
			</div>

			<br/>
			<table style="width:100%;">
			<tr class="rec_form_row">
				<td>Artist:</td>
				<td>
					<input type="text" name="artist" value="" style="width:300px;">
					<div class="info_only">Type artist here, exact spelling important.</div>
				</td>
			</tr>
			<tr class="rec_form_row">
				<td>Album&nbsp;(optional):</td>
				<td>
					<input type="text" name="album" value="" style="width:300px;">
					<div id="song_link"><a href="#" onclick="document.getElementById('song_row').style.display = 'table-row'; document.getElementById('song_link').style.display = 'none'; return false;">recommend song</a></div>
				</td>
			</tr>
			<tr class="rec_form_row" id="song_row" style="display:none;">
				<td>Song&nbsp;(optional):</td>
				<td>
					<input type="text" name="song" value="" style="width:300px;">
				</td>
			</tr>
			<tr class="rec_form_row">
				<td>Description:</td>
				<td>
					<textarea name="description" style="width:99%; height:100px;"></textarea>
				</td>
			</tr>
			<tr class="rec_form_row">
				<td></td>
				<td>
					<input type="submit" name="submit" value="Add recommendation">
				</td>
			</tr>
			</table>
		</form>
		</div>
	<?}?>

</td>
<td id="side_menu">
	<ul>
		<?if ($auth->AuthenticatedUser() != "" && $username != $auth->AuthenticatedUser()) {?>
			<li style="list-style-image: url('images/my_home01.png');"><a href="user.php?user=<?=$auth->AuthenticatedUser()?>">My recommendation page</a></li>
		<?}?>
		
		<?if ($username === $auth->AuthenticatedUser()) {?>
			<li style="list-style-image: url('images/rec_add02b.png');"><a href="#add_rec">Add new recommendation</a></li>
		<?}?>	

		<li style="list-style-image: url('images/back_home01.png');"><a href="./">Back to <?=SiteConst('site_name')?></a></li>
	</ul>

	<hr />


	<?
	$sub_ar = $dbobj->GetResultsAsNestedArray("SELECT sub.*, " .
		"(SELECT COUNT(*) FROM mr_recommendation rec WHERE rec.username = sub.subscribedToUsername AND rec.disabled = 0) AS rec_count " .
		"FROM mr_subscription sub WHERE username = " . ValToSQL_str($username) . " ORDER BY subscribedToUsername", 200);
	?>
	<h5>Subscribing to:</h5>
	<?$i = 0;
	foreach ($sub_ar as $sub_r) {
		$i++;?>
		<div class="sub_class">
			<a href="user.php?user=<?=htmlentities($sub_r["subscribedToUsername"])?>"><?=htmlentities($sub_r["subscribedToUsername"])?></a> (<?=$sub_r['rec_count']?>)
		</div>
	<?}
	if ($i == 0 && $username === $auth->AuthenticatedUser()) {?>
		<i>no subscriptions</i>
		<div class="inset_box">
			<i>Subscribe to friends' recommendations with the "Subscribe" link on users' pages.</i>
		</div>
	<?}?>


	<?
	$sub2_ar = $dbobj->GetResultsAsNestedArray("SELECT sub.username " .
		"FROM mr_subscription sub WHERE subscribedToUsername = " . ValToSQL_str($username) . " ORDER BY username", 200);
	if (sizeof($sub2_ar) > 0) {?>
		<h5 style="margin-top:20px;">Subscribed to <?=$username?>:</h5>
		<?for ($i=0; $i<sizeof($sub2_ar); $i++) {?>
			<div class="sub_class">
				<a href="user.php?user=<?=htmlentities($sub2_ar[$i]["username"])?>"><?=htmlentities($sub2_ar[$i]["username"])?></a>
			</div>
		<?}
	}?>


</td>
</tr>
</table>




<hr />

<div style="text-align:center; margin:20px auto;">
	Return to <a href="./"><?=SiteConst('site_name')?></a>
	<br/><br/><a href="http://code.google.com/p/mediarecommend/"><img src="images/poweredby_mediarecommend.png" style="border-width:0;"/></a>
</div>



<?if ($delete_mode) {?>
	<script type="text/javascript">
	alert("You are in update remove mode.  Please remove inappropriate updates using the checkboxes next to each update (if any).  Removed updates will only appear in remove-mode, and will eventually be read by a moderator.  They will then be deleted forever, or return here perminently.  Thanks for your help.");
	</script>
<?}?>

</body>
</html>
